package com.baox.agridata.synchronous.service.impl;

import com.baox.agridata.synchronous.dao.TUSERAHIAgencyMapper;
import com.baox.agridata.synchronous.entity.*;
import com.baox.agridata.synchronous.service.AgencyService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AgencyServiceImpl implements AgencyService {

    @Autowired
    private TUSERAHIAgencyMapper mapper;



    @Override
    public void dealReceiveListAHIAgencyData(List<TUSERAHIAgency> l_list) {
        if (l_list.size() > 0) {
            for (TUSERAHIAgency tuserahiAgency : l_list) {
                Integer count = mapper.selectAgencyByMid(tuserahiAgency.getMid());
                if (count > 0) {
                    mapper.updateAgencyByMid(tuserahiAgency);
                } else {
                    mapper.insertTUSERAHIAgency(tuserahiAgency);
                }
            }
        }
    }

    @Override
    public void dealReceiveListUser(UserDataToSiChuanResponse l_list) {

        try {
            List<Users> usersList = l_list.getUsers();
            usersInster(usersList);

            List<TUSERAHIUserEX> usersExList = l_list.getUserEx();
            userExInster(usersExList);

            List<TUSERAHIUserPermission> usersPrList = l_list.getUserPer();
            usersPr(usersPrList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @SneakyThrows(Exception.class)
    private void usersPr(List<TUSERAHIUserPermission> usersPrList) {
        for (TUSERAHIUserPermission tuserahiUserPermission : usersPrList) {
            Integer count = mapper.selectUserPer(tuserahiUserPermission.getMid());
            if (count<1){
                mapper.insertUserAhiUserPermission(tuserahiUserPermission);
            }else {
                mapper.updateUserPer(tuserahiUserPermission);
            }
        }
    }

    /**
     * 添加或修改用户扩展表
     * @param usersExList
     */
    @SneakyThrows(Exception.class)
    private void userExInster(List<TUSERAHIUserEX> usersExList) {
        for (TUSERAHIUserEX tuserahiUserEX : usersExList) {
            Integer count = mapper.selectUserEx(tuserahiUserEX.getMid());
            if (count<1){
                mapper.insertUserEx(tuserahiUserEX);
                String userId = mapper.selectUsersFanUserId(tuserahiUserEX.getTel());
                Integer countDep = mapper.selectUserDep(userId);
                if (countDep<1){
                    mapper.insertUserDepDate(userId,tuserahiUserEX.getMid());
                }else {
                    mapper.updateUserDep(userId,tuserahiUserEX.getMid());
                }

            }else {
                mapper.updateUserEx(tuserahiUserEX);
            }
        }

    }

    /**
     * 添加或修改用户信息
     * @param usersList
     */
    @SneakyThrows(Exception.class)
    private void usersInster(List<Users> usersList) {
        for (Users users : usersList) {
            if(users.getMobile().equals("13541263873")){
                System.out.println(users);
            }
            Integer count = mapper.selectUsers(users.getMobile());
            if (count<1){
                mapper.insertUsers(users);
            }else {
                mapper.updateUsers(users);
            }

        }


    }
}
